/****************************************************************************
 *
 * Copyright (C) 2003-2008 Los Alamos National Security, LLC
 *                         Packet Analytics Corporation
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License Version 2 as
 * published by the Free Software Foundation.  You may not use, modify or
 * distribute this program under any other version of the GNU General
 * Public License.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 *
 ****************************************************************************/
package nfse.gui.config;

import com.google.gwt.user.client.ui.*;

import com.gwtext.client.core.EventObject;
import com.gwtext.client.widgets.Button;  
import com.gwtext.client.widgets.MessageBox;  
import com.gwtext.client.widgets.Window;  
import com.gwtext.client.widgets.event.ButtonListenerAdapter;  

public class CriticalServerDialogBox extends Window {

    final TextBox ipTextBox = new TextBox();

    final CheckBox asSrcCheckBox = new CheckBox("As Source");

    final CheckBox asDstCheckBox = new CheckBox("As Destination");

    final TextArea noteTextArea = new TextArea();

    boolean cancelled = false;

    String initialIP = "";

    boolean isNew = false;
    
    ConfigurationTab configurationTab = null;

    public CriticalServerDialogBox( ConfigurationTab configTab) {
        final CriticalServerDialogBox note = this;
        this.configurationTab = configTab;
        // Set the dialog box's caption.
        FlexTable table = new FlexTable();
        // DialogBox is a SimplePanel, so you have to set it's widget property
        // to
        // whatever you want its contents to be.


        this.setShadow(true);
        this.setFrame(true);
        this.setModal(true);
        this.setIconCls("pac-icon");
        
        int idx = -1;

        table.setHTML(++idx, 0, "<b>Server IP</b>");
        table.setWidget(++idx, 0, ipTextBox);
        table.setHTML(++idx, 0, "<b>Query Mode</b>");
        table.setWidget(++idx, 0, asSrcCheckBox);
        table.setWidget(++idx, 0, asDstCheckBox);
        table.setHTML(++idx, 0, "<b>Note</b>");
        noteTextArea.setVisibleLines(5);
        table.setWidget(++idx, 0, noteTextArea);

        HorizontalPanel buttons = new HorizontalPanel();

        Button cancelButton = new Button();
        cancelButton.setText("Cancel");
        cancelButton.addListener(new ButtonListenerAdapter() {
            public void onClick(Button button, EventObject obj) {
                cancelled = true;
                note.hide();
            }
        });

        Button okButton = new Button();
        okButton.setText("OK");
        okButton.addListener(new ButtonListenerAdapter() {
            public void onClick(Button button, EventObject obj) {
                cancelled = false;
                CriticalServerEntry cse = note.getCriticalServerEntry();
                if (note.isNew()) {
                    configurationTab.addCriticalServer(cse);
                } else {
                    configurationTab.editCriticalServer(initialIP, cse);
                }
                note.hide();
            }
        });

        Button resetButton = new Button();
        resetButton.setText("Reset");
        resetButton.addListener(new ButtonListenerAdapter() {
            public void onClick(Button button, EventObject obj) {
                reset();
            }
        });

        this.add(table);
        
        this.addButton(resetButton);
        this.addButton(okButton);
        this.addButton(cancelButton);
    }

    public CriticalServerEntry getCriticalServerEntry() {
        CriticalServerEntry cse = new CriticalServerEntry();
        cse.ip = ipTextBox.getText();
        cse.note = noteTextArea.getText();
        if (asSrcCheckBox.isChecked())
            cse.asSrc = true;
        else 
            cse.asSrc = false;
        if (asDstCheckBox.isChecked())
            cse.asDst = true;
        else
            cse.asDst = false;
        return cse;
    }

    public void set(CriticalServerEntry cse) {
        noteTextArea.setText(cse.note);
        ipTextBox.setText(cse.ip);
        initialIP = cse.ip;
        if (cse.asSrc)
            asSrcCheckBox.setChecked(true);
        else
            asSrcCheckBox.setChecked(false);
        if (cse.asDst)
            asDstCheckBox.setChecked(true);
        else
            asDstCheckBox.setChecked(false);
    }

    public boolean isCancelled() {
        return cancelled;
    }

    public boolean isNew() {
        return isNew;
    }

    public void setNew(boolean isNew) {
        this.isNew = isNew;
        if (isNew)
            reset();
    }
    
    private void reset() {
        noteTextArea.setText("");
        ipTextBox.setText("");
        asSrcCheckBox.setChecked(true);
        asDstCheckBox.setChecked(true);
    }
}
